package com.sx.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sx.bean.Resource;
import com.sx.dao.ResourceDao;
import com.sx.service.ResourceService;
import com.sx.util.Pagination;
@Service()
public class ResourceServiceImpl implements ResourceService{
	@Autowired
	private ResourceDao resourceDao;
	@Override
	public void addResource(Resource resource) {
		// TODO Auto-generated method stub
		if (resource.getRes_url() == null && resource.getRes_url().trim().length() == 0)
			throw new RuntimeException("增加资源: 空参");
		if (resource.getDir_code() == null && resource.getDir_code().trim().length() == 0)
			throw new RuntimeException("增加资源: 空参");
		if (resource.getDir_name() == null && resource.getDir_name().trim().length() == 0)
			throw new RuntimeException("增加资源: 空参");
		if (resource.getDir_level_number() == null && resource.getDir_level_number().trim().length() == 0)
			throw new RuntimeException("增加资源: 空参");
		if (resource.getIsitem() == null && resource.getIsitem().trim().length() == 0)
			throw new RuntimeException("增加资源: 空参");
		if (resource.getDir_order() == null)
			throw new RuntimeException("增加资源: 空参");
		if ("".equals(resource.getParent_id().trim()))
			resource.setParent_id("root");
		resource.setRes_uuid(UUID.randomUUID().toString());
		resourceDao.insert(resource);
	}
	@Override
	public Pagination getResourcePage(Integer pageSize, Integer pageNumber) {
		// TODO Auto-generated method stub
		Pagination<Resource> pagination = new Pagination<Resource>(pageNumber, pageSize);
		List<Resource> list = resourceDao.list((pageNumber - 1) * pageSize, pageSize);
		int count = resourceDao.selectCount();
		pagination.setTotal(count);
		pagination.setRows(list);
		return pagination;
	}
	@Override
	public Resource getResource(String uuid) {
		// TODO Auto-generated method stub
		Resource resource = resourceDao.selectById(uuid);
		return resource;
	}
	@Override
	public void updateResource(Resource resource) {
		// TODO Auto-generated method stub
		resourceDao.update(resource);
	}
	@Override
	public void remove(String id) {
		// TODO Auto-generated method stub
		resourceDao.deleteById(id);
	}

}
